from src.core import Enums
from src.db import dbs, dbm


def search_app_by_id(app_id):
    """根据应用ID搜索应用信息"""
    with dbm.get("hosting_Java") as db_session:
        from sqlalchemy.orm import scoped_session
        db_session: scoped_session
        db_result = (
            db_session.query(dbs.hosting_Java.App)
            .filter(dbs.hosting_Java.App.app_id == app_id)
            .first()
        )
        if db_result:
            return db_result
    with dbm.get("hosting_Static") as db_session:
        db_result = (
            db_session.query(dbs.hosting_Static.App)
            .filter(dbs.hosting_Static.App.app_id == app_id)
            .first()
        )
        if db_result:
            return db_result
        return None


def search_stype_by_id(app_id):
    """根据应用ID搜索应用类型"""
    with dbm.get("hosting_Java") as db_session:
        from sqlalchemy.orm import scoped_session
        db_session: scoped_session
        db_result = (
            db_session.query(dbs.hosting_Java.App)
            .filter(dbs.hosting_Java.App.app_id == app_id)
            .first()
        )
        if db_result:
            return Enums.Hosting.Stypes.JAVA.value
    with dbm.get("hosting_Static") as db_session:
        db_result = (
            db_session.query(dbs.hosting_Static.App)
            .filter(dbs.hosting_Static.App.app_id == app_id)
            .first()
        )
        if db_result:
            return Enums.Hosting.Stypes.WEB.value
        return None


def search_stype_by_back_id(app_id):
    """根据应用ID搜索应用类型"""
    with dbm.get("hosting_Java") as db_session:
        from sqlalchemy.orm import scoped_session
        db_session: scoped_session
        db_result = (
            db_session.query(dbs.hosting_Java.App_backup)
            .filter(dbs.hosting_Java.App.app_id == app_id)
            .first()
        )
        if db_result:
            return Enums.Hosting.Stypes.JAVA.value
    with dbm.get("hosting_Static") as db_session:
        db_result = (
            db_session.query(dbs.hosting_Static.App_backup)
            .filter(dbs.hosting_Static.App.app_id == app_id)
            .first()
        )
        if db_result:
            return Enums.Hosting.Stypes.WEB.value
        return None


def search_max_structure_num_by_app_id(app_id: str) -> int:
    """根据备份ID搜索最大结构数"""
    with dbm.get("hosting_Java") as db_session:
        from sqlalchemy.orm import scoped_session
        db_session: scoped_session
        db_result = (
            db_session.query(dbs.hosting_Java.App_backup)
            .filter(dbs.hosting_Java.App_backup.app_id == app_id)
            .all()
        )
        if db_result:
            return len(db_result)
    with dbm.get("hosting_Static") as db_session:
        db_result = (
            db_session.query(dbs.hosting_Static.App_backup)
            .filter(dbs.hosting_Static.App_backup.app_id == app_id)
            .all()
        )
        if db_result:
            return len(db_result)
        return 0


def search_all_apps():
    """搜索所有应用"""
    result = []
    with dbm.get("hosting_Java") as db_session:
        from sqlalchemy.orm import scoped_session
        db_session: scoped_session
        db_result = db_session.query(dbs.hosting_Java.App).all()
        for i in db_result:
            result.append(i.app_id)
    with dbm.get("hosting_Static") as db_session:
        db_result = db_session.query(dbs.hosting_Static.App).all()
        for i in db_result:
            result.append(i.app_id)
        return result
